//
//  HistoryKeyView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/16.
//

import UIKit

class HistoryKeyView: UIView {
    var hiddenClose:Bool{
        set{btn.isHidden = newValue}
        get{return btn.isHidden}
    }
    private let btn = UIButton(type: .custom)
    private var index = 0
    private let line = UIView()

    var delegate:HistoryKeyViewDelegate?
    init(item:NewsSearchTextModel,index:Int) {
        super.init(frame: .zero)
        self.index = index
        let label = UILabel()
        addSubview(label)
        label.textColor = B000_COLOR
        label.font = Font_Size_15
        label.text = "\(item.key):|\(index)"
        let icon =  UIImage(named: "close")?.withRenderingMode(.alwaysTemplate)
        btn.tintColor = B999_TITLE_COLOR
        btn.setImage(icon, for: .normal)
        addSubview(btn)
        btn.snp.makeConstraints({
            $0.width.height.equalTo(15)
            $0.right.equalToSuperview().inset(15)
            $0.centerY.equalToSuperview()
        })
        label.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.top.bottom.equalTo(0)
            $0.right.equalTo(btn.snp.left)
        })
        line.backgroundColor = Separator_COLOR
        addSubview(line)
        line.snp.makeConstraints({
            $0.width.equalTo(1)
            $0.top.right.bottom.equalTo(0)
        })
        line.isHidden = index % 2 == 0 ? false : true
        addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(deleteSelf)))
    }
    func updateLayout(index:Int) {
        self.index = index
        line.isHidden = index % 2 == 0 ? false : true
    }
    @objc func deleteSelf(){
        if !hiddenClose {
            self.delegate?.deleteKey(index: index)
        }
    }
    required init?(coder: NSCoder) {
        fatalError()
    }

}
protocol HistoryKeyViewDelegate {
    func deleteKey(index:Int)
}
